<?php class carts extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('cart');
        $this->load->library("loadpage");
    }
/*
    public function load_page($namepage, $data) {
        if ($this->session->userdata('is_logged_in')) {
            $data['link'] = '';
            $data['link_logout'] = base_url() . 'index.php/welcome/dangxuat';
            $data['log_out'] = 'ĐĂNG XUẤT';
            $data['text_before'] = 'TÀI KHOẢN';
            $data['text_after'] = $this->session->userdata('email');
            $data['include'] = 'member.php';
            $this->load->view($namepage, $data);
        } else {
            $data['link'] = base_url() . 'index.php/welcome/dangky';
            $data['link_logout'] = '';
            $data['log_out'] = '';
            $data['text_before'] = 'ĐĂNG NHẬP';
            $data['text_after'] = 'ĐĂNG KÝ';
            $data['include'] = 'login.php';
            if ($namepage === 'trangchu' || $namepage === 'dangky') {
                $this->load->view($namepage, $data);
            }
            else
                $this->load->view('error', $data);
        }
    }
*/
    public function index() {

        $this->load->model('mtacpham');
        $data = array();
         $lp = new loadpage();
        $data['matps'] = array();
        $data['tentps'] = array();
        $data['giabans'] = array();
        $data['soluongs'] = array();
        $data['hinhanhs']=array();
        $giohang = $this->cart->contents();
        foreach ($giohang as $row) {
            $sp = new Mtacpham();
            $matp = $row['id'];
            $sp->getObject("matp='$matp'");
            array_push(  $data['matps'],$matp);
            array_push($data['tentps'], $sp->get_tentp());
            array_push($data['giabans'], $sp->get_giaban());
            array_push($data['soluongs'], $row["qty"]);
            array_push($data['hinhanhs'], $sp->get_hinhanh());
        }
        $lp->load(array('showcart'), $data);
        //$this->load->view('showcart', $data);
    }

    public function add() {
        $id = trim($_GET['id']);
        $qty = trim($_GET['qty']);
        $price = trim($_GET['price']);
        $name = trim($_GET['name']);
        if ($this->check($id, $qty)) {
            $data2 = array(
                'id' => $id,
                'qty' => $qty,
                'price' => $price,
                'name' => $name
            );

            $this->cart->insert($data2);
        }
        $this->load->view('listsanpham_view');
    }

    function check($id, $qty) {
        $data = $this->cart->contents();
        foreach ($this->cart->contents() as $item) {
            if ($item['id'] == $id) {
                // echo $item['id'] . $id;
                $num = intval($item['qty']) + intval($qty);
                $cartitem = array('rowid' => $item['rowid'], 'qty' => $num);
                $this->cart->update($cartitem);
                return false;
            }
        }
        return true;
    }

    public function update() {
        $rowid = trim($_GET['rowid']);
        $qty = trim($_GET['qty']);
        $dt = array('rowid' => $rowid, 'qty' => $qty);
        $this->cart->update($dt);
        $this->load->view('listsanpham_view');
    }

    public function delete() {
        $rowid = trim($_GET['rowid']);
        $qty = 0;
        $dt = array('rowid' => $rowid, 'qty' => $qty);
        $this->cart->update($dt);
        $this->load->view('listsanpham_view');
    }

    public function show() {
        $this->load->view('listsanpham_view');
    }

    public function remove() {
        $this->cart->destroy();
    }

    public function address() {

        $this->load->model('KHACHHANG');
        $data = array();
        $email = $this->session->userdata('email');
        $kh = new Khachhang();
        $temp = $kh->getListObject("*", "email='$email'");
        $data['diachi'] = $temp;

        $this->load->model('KHACHHANG');

        $data = array();
        $data['phi'] = $this->input->get('phi');
        $email = $this->session->userdata('email');
        $kh = new Khachhang();
        $temp = $kh->getListObject("*", "email='$email'");
        $data['diachi'] = $temp;

        $this->load_page('address', $data);
    }

    public function printtopdf() {

        $this->load->library('Mpdf');
        $mpdf = new Pdf();
        $mpdf->SetAutoFont(AUTOFONT_ALL);
        $mpdf->SetHeader('{DATE j-m-Y}|AIOShop.vn|{PAGENO}');

        $dt = array();
        $dt['mpdf'] = $mpdf;
        $this->load->view('in', $dt);
    }

}

?>